﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TabbedDetails : System.Web.UI.UserControl
{
	public static int pacientTabIndex = 0;
	public static int admissionTabIndex = 1;
	public static int checkinTabIndex = 2;
	public static int checkupTabIndex = 3;
	public static int checkoutTabIndex = 4;

	public static string[] action = new string[] { "view", "admit", "checkin", "checkup", "checkout" };

	public static bool SetUrl(Control c, string url)
	{
		return SetUrl(c.Page, url);
	}
	public static bool SetUrl(Page p, string url)
	{
		Label lblPageUrl = FindControl(p.Form, "lblPageUrl") as Label;
		if (lblPageUrl != null) lblPageUrl.Text = url; else return false;
		ScriptManager.RegisterStartupScript(p, typeof(Page), "SetUrl", "DisplayMessageIn('" + lblPageUrl.ClientID + "', \"" + url.Replace(@"\", @"\\") + "\", '', false);", true);
		return true;
	}
	private static Control FindControl(Control root, string id)
	{
		foreach (Control c in root.Controls)
			if (c.ID.Equals(id))
				return c;
		foreach (Control c in root.Controls)
		{
			Control result = FindControl(c, id);
			if (result != null) return result;
		}
		return null;
	}
	public static bool ShowUrl(Control source, params string[] queryString)
	{
		System.Collections.Hashtable qs = GetPairs( source.Page.Session["customQueryString"]);
		System.Text.StringBuilder builtQueryString = new System.Text.StringBuilder();

        for (int i = 0; i < queryString.Length; i += 2)
			if (!string.IsNullOrEmpty(queryString[i + 1]))
				qs[queryString[i]] = queryString[i + 1];

        foreach (object key in qs.Keys)
		{
			builtQueryString.Append(builtQueryString.Length == 0 ? '?' : '&');
			builtQueryString.Append(key);
			builtQueryString.Append('=');
			builtQueryString.Append(qs[key]);
		}
		string result = builtQueryString.ToString();
		source.Page.Session["customQueryString"] = result;
		return SetUrl(source, source.Page.Request.Path + result);
	}

	private static System.Collections.Hashtable GetPairs(object queryString)
	{
		System.Collections.Hashtable result = new System.Collections.Hashtable();
		if(queryString == null)
			return result;
		string[] pairs = queryString.ToString().TrimStart('?').Split('&');
		foreach (string pair in pairs)
			if (pair.Trim().Length > 0)
			{
				string[] kv = pair.Split('=');
				result[kv[0]] = kv[1];
			}
		return result;
	}

	protected void Page_Load(object sender, EventArgs e)
    {
    }
}
